home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / Mark Pilgrim / Brailler 0.5ß / Brailler 0.5ß.source / Shell ƒ / integrity.c < prev    next >
Encoding:
Text File  |  1994-10-30  |  4.4 KB  |  116 lines  |  [TEXT/MMCC]

  1. g program launch).  Returns FALSE if program integrity is
  2.    not verified; programIntegritySet is TRUE if this procedure installs the
  3.    integrity checker. */
  4. {
  5.     short            thisFile;
  6.     long            count;
  7.     long            resDataLength, checkData;
  8.     long            resMapLength, checkMap;
  9.     long            resMapOffset;
  10.     short            resAttributes;
  11.     long            tag;
  12.     KeyMap            rawKeys;
  13.     unsigned short    theKeys[8];
  14.     
  15.     *programIntegritySet=FALSE;
  16.     GetKeys(rawKeys);
  17.     Mymemcpy((Ptr)theKeys, (Ptr)rawKeys, sizeof(rawKeys));
  18.     FlushVol(0L, 0);        /* just to be on the safe side */
  19.     
  20.     /* interestingly enough, the new-and-improved functions to open a resource fork
  21.        (HOpenRF & FSpOpenRF) won't work here.  They check permissions and won't
  22.        allow write permission to an already-open resource fork -- so we won't be
  23.        able to install the integrity checker on the fly if we Do The Right Thing™.
  24.        Luckily, the old-and-inferior function (OpenRF) doesn't ask permission... */
  25.     OpenRF(LMGetCurApName(), 0, &thisFile);
  26.     
  27.     SetFPos(thisFile, 1, RESOURCE_FORK_DATA_LENGTH_OFFSET);
  28.     count=4L;
  29.     /* get length of resource data */
  30.     if (FSRead(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  31.         return FALSE;
  32.     
  33.     SetFPos(thisFile, 1, RESOURCE_FORK_MAP_LENGTH_OFFSET);
  34.     count=4L;
  35.     /* get length of resource map */
  36.     if (FSRead(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  37.         return FALSE;
  38.     
  39.     SetFPos(thisFile, 1, TAG_OFFSET);
  40.     count=4L;
  41.     FSRead(thisFile, &count, (Ptr)(&tag));    /* read long-word tag */
  42.     if (tag!=THE_TAG)    /* if not equal to our tag, integrity checker not installed */
  43.     {
  44.         if (theKeys[3]&1)    /* if SHIFT key down, install integrity checker */
  45.         {
  46.             count=4L;
  47.             SetFPos(thisFile, 1, 4L);
  48.             /* get the offset of the start of the resource map in the resource fork */
  49.             if (FSRead(thisFile, &count, (Ptr)(&resMapOffset))!=noErr)
  50.                 return FALSE;
  51.             
  52.             resMapOffset+=22L;    /* resource fork attributes at map+22 */
  53.             count=2L;
  54.             SetFPos(thisFile, 1, resMapOffset);
  55.             /* get old resource fork attributes */
  56.             if (FSRead(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  57.                 return FALSE;
  58.             
  59.             resAttributes|=0x8000;    /* lock resource fork */
  60.             count=2L;
  61.             SetFPos(thisFile, 1, resMapOffset);
  62.             /* rewrite new resource fork attributes */
  63.             /* It is a little-known fact that this trick makes the application
  64.                immune to all known viruses -- no known virus bothers to unlock the
  65.                resource fork before attempting to infect, and ABSOLUTELY NO changes
  66.                can be made to the resource fork when it's locked like this.  On the
  67.                other hand, you better be damn sure that the application has no
  68.                viruses in it when you install the integrity checker -- if it's
  69.                infected when this code is run, you won't be able to disinfect it
  70.                until you unlock the resource fork. */
  71.             if (FSWrite(thisFile, &count, (Ptr)(&resAttributes))!=noErr)
  72.                 return FALSE;
  73.             
  74.             SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  75.             count=4L;
  76.             /* store length of resource data */
  77.             if (FSWrite(thisFile, &count, (Ptr)(&resDataLength))!=noErr)
  78.                 return FALSE;
  79.             
  80.             SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  81.             count=4L;
  82.             /* store length of resource map */
  83.             if (FSWrite(thisFile, &count, (Ptr)(&resMapLength))!=noErr)
  84.                 return FALSE;
  85.             
  86.             SetFPos(thisFile, 1, TAG_OFFSET);
  87.             count=4L;
  88.             tag=THE_TAG;
  89.             /* store tag so we know integrity checker is installed */
  90.             if (FSWrite(thisFile, &count, (Ptr)(&tag))!=noErr)
  91.                 return FALSE;
  92.             
  93.             *programIntegritySet=TRUE;        /* so we know we've installed it */
  94.         }
  95.         
  96.         return TRUE;
  97.     }
  98.     else
  99.     {
  100.         SetFPos(thisFile, 1, STORED_DATA_LENGTH_OFFSET);
  101.         count=4L;
  102.         /* get stored resource data length */
  103.         if (FSRead(thisFile, &count, (Ptr)(&checkData))!=noErr)
  104.             return FALSE;
  105.         
  106.         SetFPos(thisFile, 1, STORED_MAP_LENGTH_OFFSET);
  107.         count=4L;
  108.         /* get stored resource map length */
  109.         if (FSRead(thisFile, &count, (Ptr)(&checkMap))!=noErr)
  110.             return FALSE;
  111.         
  112.         /* check real resource data/map lengths to stored values */
  113.         return ((resDataLength==checkData) && (resMapLength==checkMap));
  114.     }
  115. }
  116.